import { NextRequest, NextResponse } from "next/server";
const mysql = require("mysql2/promise");
// 创建数据池
const pool = mysql.createPool({
  connectionLimit: 10,
  host: "127.0.0.1", // 数据库地址
  user: "root", // 数据库用户
  password: "123456", // 数据库密码
  database: "user_data", // 选中数据库
});

export async function GET(request: NextRequest) {
  try {
    // 获取查询字符串中的 sqlStr 参数
    const sqlStr = request.nextUrl.searchParams.get("sqlStr");
    

    // 从连接池中获取连接
    const connection = await pool.getConnection();

    // 执行 MySQL 查询
    const [rows, fields] = await connection.query(sqlStr); //'SELECT * FROM user_info'

    // 释放连接回连接池
    connection.release();

    return NextResponse.json({ data: rows }, { status: 200 });
  } catch (error) {
    console.error("Error:", error);
    return NextResponse.json(
      { error: "Internal Server Error" },
      { status: 500 }
    );
  }
}
